/*********************/
/** DataItemUtils.java */
/*********************/
package atag.apps.mainApp;

import atag.j2mespecific.*;
import atag.runtime.DataItem;
import java.io.*;
import java.util.Vector;


public class DataItemUtils{
    
    public static void serializeData(DataOutput dg, Vector core) throws IOException {
        if (core.elementAt(0) instanceof LightReading){
            dg.writeInt(IDConstants.D_LIGHTREADING);
            ((LightReading)core.elementAt(0)).serialize(dg);
		}else if (core.elementAt(0) instanceof Action){
            dg.writeInt(IDConstants.D_ACTION);
            ((Action)core.elementAt(0)).serialize(dg);
		}else if (core.elementAt(0) instanceof RFIDLightSetting){
            dg.writeInt(IDConstants.D_RFIDLIGHTSETTING);
            ((RFIDLightSetting)core.elementAt(0)).serialize(dg);
		}else if (core.elementAt(0) instanceof ControllerLightSetting){
            dg.writeInt(IDConstants.D_CONTROLLERLIGHTSETTING);
            ((ControllerLightSetting)core.elementAt(0)).serialize(dg);
		}else if (core.elementAt(0) instanceof RFID){
            dg.writeInt(IDConstants.D_RFID);
            ((RFID)core.elementAt(0)).serialize(dg);
		}else if (core.elementAt(0) instanceof LightInfoRequest){
            dg.writeInt(IDConstants.D_LIGHTINFOREQUEST);
            ((LightInfoRequest)core.elementAt(0)).serialize(dg);
		}else if (core.elementAt(0) instanceof LightInfoResponse){
            dg.writeInt(IDConstants.D_LIGHTINFORESPONSE);
            ((LightInfoResponse)core.elementAt(0)).serialize(dg);
		}
    }
    
    public static void deserializeData(DataInput dg, Vector core) throws IOException,
            ClassNotFoundException, IllegalAccessException, InstantiationException {
        switch(dg.readInt()){
            case IDConstants.D_LIGHTREADING:
                LightReading newLightReading = new LightReading();
                newLightReading.deserialize(dg);
                core.addElement(newLightReading);
                break;
            case IDConstants.D_ACTION:
                Action newAction = new Action();
                newAction.deserialize(dg);
                core.addElement(newAction);
                break;
            case IDConstants.D_RFIDLIGHTSETTING:
                RFIDLightSetting newRFIDLightSetting = new RFIDLightSetting();
                newRFIDLightSetting.deserialize(dg);
                core.addElement(newRFIDLightSetting);
                break;
            case IDConstants.D_CONTROLLERLIGHTSETTING:
                ControllerLightSetting newControllerLightSetting = new ControllerLightSetting();
                newControllerLightSetting.deserialize(dg);
                core.addElement(newControllerLightSetting);
                break;
            case IDConstants.D_RFID:
                RFID newRFID = new RFID();
                newRFID.deserialize(dg);
                core.addElement(newRFID);
                break;
            case IDConstants.D_LIGHTINFOREQUEST:
                LightInfoRequest newLightInfoRequest = new LightInfoRequest();
                newLightInfoRequest.deserialize(dg);
                core.addElement(newLightInfoRequest);
                break;
            case IDConstants.D_LIGHTINFORESPONSE:
                LightInfoResponse newLightInfoResponse = new LightInfoResponse();
                newLightInfoResponse.deserialize(dg);
                core.addElement(newLightInfoResponse);
                break;
            default:
                System.err.println("Unknown data item type attempted to be deserialized");
        }
    }
}